package com.gzhryc.shared_device.oem.code.agent.dao;

import java.sql.SQLException;
import java.util.List;

import com.gzhryc.jdbc.BaseDao;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.Query;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentReferrerBusinessConfig;
import com.gzhryc.shared_device.oem.code.agent.dao.models.AgentReferrerCount;
import com.gzhryc.shared_device.oem.code.agent.dao.models.AgentReferrerUser;

public class AgentReferrerBusinessConfigDao extends BaseDao<AgentReferrerBusinessConfig> {

    public AgentReferrerBusinessConfigDao(String jdbcKey){
        super(jdbcKey);
    }

    public List<AgentReferrerUser> findGroupReferrerUserId(Conditions conditions,Integer pageNo,Integer pageLimit) throws SQLException{
    	 String sql = "SELECT t1.* FROM (SELECT t.referrer_user_id AS referrerUserId,COUNT(distinct user_id) AS agentCount FROM " + this.tableName
                 + " t INNER JOIN user_info user ON t.referrer_user_id = user.id";
         Query query = new Query(sql);
         query.appendSql(conditions.toSql(query.getParams(),"t",true));
         query.appendSql(" GROUP BY referrer_user_id) t1");
         query.setPageNo(pageNo);
         query.setPageLimit(pageLimit);
         return this.jdbcHelper.find(query,AgentReferrerUser.class);
    }
    
    public Long countGroupReferrerUserId(Conditions conditions) throws SQLException{
    	 String sql = "SELECT COUNT(1) FROM (SELECT t.referrer_user_id AS referrerUserId,COUNT(distinct user_id) AS agentCount FROM " + this.tableName
                 + " t INNER JOIN user_info user ON t.referrer_user_id = user.id";
         Query query = new Query(sql);
         query.appendSql(conditions.toSql(query.getParams(),"t",true));
         query.appendSql(" GROUP BY referrer_user_id) t1");

         return this.jdbcHelper.getFirstField(query, Long.class);
    }

    public List<AgentReferrerCount> countGroupUserId(Conditions conditions,Integer pageNo,Integer pageLimit) throws SQLException {
        String sql = "SELECT t1.* FROM (SELECT t.user_id AS userId,COUNT(1) AS configCount FROM " + this.tableName
                + " t INNER JOIN user_info user ON t.user_id = user.id";
        Query query = new Query(sql);
        query.appendSql(conditions.toSql(query.getParams(),"t",true));
        query.appendSql(" GROUP BY user_id) t1");
        query.setPageNo(pageNo);
        query.setPageLimit(pageLimit);
        return this.jdbcHelper.find(query,AgentReferrerCount.class);
    }
}
